//
//  MDRecommendViewController.swift
//  麦兜直播
//
//  Created by Apple on 16/10/9.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

private let margin:CGFloat = 10.0
private let NORMALID = "MDNormalCellID"
private let BEAUTYID = "MDBeautyCellID"
private let TITLEBARID = "MDTitleBarViewID"
private let itemW = (screenWidth - 3*margin)*0.5
import UIKit

class MDRecommendViewController: UIViewController {
    
    lazy var collectionView:UICollectionView = {[unowned self] in
        let layout = UICollectionViewFlowLayout()
        let itemH = itemW*3/4
        layout.itemSize = CGSize(width:itemW , height: itemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.contentInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        
        collectionView.register(UINib(nibName: "MDNormalCell", bundle: nil), forCellWithReuseIdentifier: NORMALID)
        collectionView.register(UINib(nibName: "MDBeautyCell", bundle: nil), forCellWithReuseIdentifier: BEAUTYID)
        print(UINib(nibName: "MDTitleBarView", bundle: nil))
        collectionView.register(UINib(nibName: "MDTitleBarView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: TITLEBARID)

        return collectionView
        
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        self.collectionView.reloadData()
    }

}


extension MDRecommendViewController{
    fileprivate func setUpUI(){
        view.addSubview(self.collectionView)
    }
}


// MARK:- 数据源和代理方法
extension MDRecommendViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    func numberOfSections(in collectionView: UICollectionView) -> Int{
    
        return 10
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if (indexPath as NSIndexPath).section == 1{
            let  beautyCell  = collectionView.dequeueReusableCell(withReuseIdentifier: BEAUTYID, for: indexPath)
            beautyCell.backgroundColor = UIColor.getRandomColor()
            return beautyCell
            
        }else{
            let normalCell = collectionView.dequeueReusableCell(withReuseIdentifier: NORMALID, for: indexPath)
            normalCell.backgroundColor = UIColor.getRandomColor()
            return normalCell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: TITLEBARID, for: indexPath) as! MDTitleBarView
        print(headView)
        headView.backgroundColor = UIColor.red
        return headView
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if (indexPath as NSIndexPath).section == 1 {
            return CGSize(width: itemW, height: itemW*4/3)
        }
        
        return CGSize(width: itemW, height: itemW*3/4)
    }
}
